package cn.imu.appdevexperiment2020.cch.cchactivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.cch.retrofit.api.ApiClient;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.RegisterResultBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.RegisterSendBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.ResetPwdResultBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.ResetPwdSendBean;
import cn.imu.appdevexperiment2020.cch.util.AuthUtil;
import cn.imu.appdevexperiment2020.cch.util.ToastUtil;
import cn.imu.appdevexperiment2020.common.BaseActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-12-25 16:09
 */
public class ResetPwdActivity extends BaseActivity {
   private static final String TAG = "ResetPwdActivity";
   @BindView(R.id.tv_title)
   TextView tvtitle;

   @BindView(R.id.edit_til_password)
   TextInputLayout edit_til_password;
   @BindView(R.id.edit_til_repassword)
   TextInputLayout edit_til_repassword;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_editpassword);

      initView();
//      init();
   }

   private void init() {

   }

   private void initView() {
      tvtitle.setText("修改密码");
   }

   @OnClick(R.id.img_title_back)
   public void img_title_back(){
      finish();
   }
   @OnClick(R.id.edit_btn_editpassword)
   public void editpwd(){
      String password=edit_til_password.getEditText().getText().toString();
      String repassword=edit_til_repassword.getEditText().getText().toString();
      if (password.equals(repassword)){
         ResetPwdSendBean resetPwdSendBean = new ResetPwdSendBean();
         resetPwdSendBean.setPassword(password);
         resetPwdSendBean.setUpdateBy("cch");
         SharedPreferences sharedPreferences =getSharedPreferences("cch", Context.MODE_PRIVATE);

         resetPwdSendBean.setUserId(sharedPreferences.getString("userId",""));

         Call<ResetPwdResultBean> call = ApiClient.getInstance(getApplicationContext())
                 .getReturnData(AuthUtil.GetAuth(),resetPwdSendBean);
         call.enqueue(new Callback<ResetPwdResultBean>() {
            @Override
            public void onResponse(Call<ResetPwdResultBean> call, Response<ResetPwdResultBean> response) {
               if (response.body()!=null){
                  if (response.body().getCode()==200){
                     Log.e(TAG,response.body().getMsg().toString());
                     ToastUtil.ShowToast(getApplicationContext(),"修改成功");
                     finish();
                  }else {
                     ToastUtil.ShowToast(getApplicationContext(),response.body().getMsg().toString());
                  }
               }else{
                  ToastUtil.ShowToast(getApplicationContext(),"网络请求错误");
               }
            }


            @Override
            public void onFailure(Call<ResetPwdResultBean> call, Throwable t) {

            }
         });

      }else{
         ToastUtil.ShowToast(this,"两次输入的密码不一致");
      }
   }
}
